package com.iemes.controller.finance;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.CustomerCutMaterialDetailFormMap;
import com.iemes.entity.CustomerCutMaterialFormMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.PcontractTrackingDetailFormMap;
import com.iemes.entity.PcontractTrackingFormMap;
import com.iemes.mapper.finance.CustomerCutMaterialDetailMapper;
import com.iemes.mapper.finance.CustomerCutMaterialMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@RequestMapping("/finance")
@Controller
public class CustomerCutMaterialController extends BaseController {

	@Inject
	private CustomerCutMaterialMapper customerCutMaterialMapper;
	
	@Inject
	private CustomerCutMaterialDetailMapper customerCutMaterialDetailMapper;
	
	@RequestMapping("customerCutMaterial")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/customerCutMaterial_list";
	}
	
	
	@ResponseBody
	@RequestMapping("customerCutMaterial_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		CustomerCutMaterialFormMap customerCutMaterialFormMap=getFormMap(CustomerCutMaterialFormMap.class);

		List<Map<String,Object>> customerCutMaterialFormMaps=customerCutMaterialMapper.getCustomerCutMaterialByCondition(customerCutMaterialFormMap);
		
		for(Map<String,Object>c:customerCutMaterialFormMaps){
			if(Integer.parseInt(c.get("whetherOutsourcing").toString())==0){
				c.put("whetherOutsourcing", "否");
			}else{
				c.put("whetherOutsourcing", "是");	
			}
		}
		LuogicPaging(pageNow, pageSize, customerCutMaterialFormMaps);
		pageView.setRecords(customerCutMaterialFormMaps);
		return pageView;
	}
	
	
	@RequestMapping("customerCutMaterial_addUI")
	public String addUI(Model model) throws Exception {
		
		//显示供应商
		List<BrandFormMap> brandFormMap=baseMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brandFormMap", brandFormMap);
		
		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/customerCutMaterial_add";
	}
	
	@ResponseBody
	@RequestMapping("customerCutMaterial_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="客户割料量统计",methods="原料-记录割料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		CustomerCutMaterialFormMap customerCutMaterialFormMap = getFormMap(CustomerCutMaterialFormMap.class);
		customerCutMaterialFormMap.put("state", 0);
		customerCutMaterialMapper.addEntity(customerCutMaterialFormMap);

		// 采购合同子项
		// int pId=pcontractTrackingMapper.getMaxId();
		// String[] itemid = request.getParameterValues("item_no_1");
		// String[] itemUseNumberArr = request.getParameterValues("item_time");
		// String[] itemBalanceUpArr =
		// request.getParameterValues("item_balance_up_1");
		// String[] itemBalanceDownArr =
		// request.getParameterValues("item_balance_down_1");
		//int cid=Integer.parseInt(request.getParameter("customerCutMaterialFormMap.id"));
		String[] itemBalanceSitArr = request.getParameterValues("item_balance_sit_1");
		String[] itemBalanceNumArr = request.getParameterValues("item_balance_num_1");
		if (null != itemBalanceSitArr) {
			for (int i = 0; i < itemBalanceSitArr.length; i++) {
				CustomerCutMaterialDetailFormMap customerCutMaterialDetailFormMap = new CustomerCutMaterialDetailFormMap();
				customerCutMaterialDetailFormMap.put("customerCutMaterialId",customerCutMaterialFormMap.get("id"));
				customerCutMaterialDetailFormMap.put("materialsFor",Double.parseDouble(itemBalanceSitArr[i]));
				customerCutMaterialDetailFormMap.put("residueNumber",Double.parseDouble(itemBalanceNumArr[i]));
				customerCutMaterialDetailFormMap.put("state", 0);
				baseMapper.addEntity(customerCutMaterialDetailFormMap);
			}

		}

		return "success";
	}
	
	
	@RequestMapping("customerCutMaterial_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		//获取物料对象
		CustomerCutMaterialFormMap customerCutMaterialFormMap = customerCutMaterialMapper.getCustomerCutMaterialById(Integer.parseInt(id));
		model.addAttribute("customerCutMaterialFormMap", customerCutMaterialFormMap);
		//显示供应商
		List<BrandFormMap> brandFormMap=baseMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brandFormMap", brandFormMap);
		
		//显示子项
		List<CustomerCutMaterialDetailFormMap> customerCutMaterialDetailFormMap=customerCutMaterialDetailMapper.getAll(customerCutMaterialFormMap.getInt("id"));
		model.addAttribute("customerCutMaterialDetailFormMap", customerCutMaterialDetailFormMap);

		return Common.BACKGROUND_PATH + "/finance/purchasingmanagement/customerCutMaterial_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="customerCutMaterial_editEntity")
	@SystemLog(module="客户割料量统计",methods="原料-修改割料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		CustomerCutMaterialFormMap customerCutMaterialFormMap=getFormMap(CustomerCutMaterialFormMap.class);
		customerCutMaterialMapper.editEntity(customerCutMaterialFormMap);
		
		//先删除
		baseMapper.deleteByAttribute("customerCutMaterialId","'"+ Integer.parseInt(customerCutMaterialFormMap.getStr("id")) +"'", CustomerCutMaterialDetailFormMap.class);
		
		// 采购合同子项
		// int pId=pcontractTrackingMapper.getMaxId();
		//String[] itemid = request.getParameterValues("item_no_1");
		//String[] itemUseNumberArr = request.getParameterValues("item_time");
		//String[] itemBalanceUpArr = request.getParameterValues("item_balance_up_1");
		//String[] itemBalanceDownArr = request.getParameterValues("item_balance_down_1");
		String[] itemBalanceSitArr = request.getParameterValues("item_balance_sit_1");
		String[] itemBalanceNumArr = request.getParameterValues("item_balance_num_1");
		if (null != itemBalanceSitArr) {
			for (int i = 0; i < itemBalanceSitArr.length; i++) {
				CustomerCutMaterialDetailFormMap customerCutMaterialDetailFormMap = new CustomerCutMaterialDetailFormMap();
				customerCutMaterialDetailFormMap.put("customerCutMaterialId",Integer.parseInt(customerCutMaterialFormMap.getStr("id")));
				customerCutMaterialDetailFormMap.put("materialsFor", Double.parseDouble(itemBalanceSitArr[i]));
				customerCutMaterialDetailFormMap.put("residueNumber",Double.parseDouble(itemBalanceNumArr[i]));
				customerCutMaterialDetailFormMap.put("state", 0);
				baseMapper.addEntity(customerCutMaterialDetailFormMap);
			}

		}
		
		return "success";
	}
	
	
	
	@ResponseBody
	@RequestMapping(value="customerCutMaterial_deleteEntity")
	@SystemLog(module="客户割料量统计",methods="割料-删除原料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		
		CustomerCutMaterialFormMap customerCutMaterialFormMap=getFormMap(CustomerCutMaterialFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		
		for(int i=0;i<str.length;i++){
			System.out.println("循环打印"+str[i]+"呱呱");
			customerCutMaterialFormMap.set("state", 1);
			customerCutMaterialFormMap.put("id", str[i]);
			customerCutMaterialDetailMapper.editEntity(customerCutMaterialFormMap);
			
			List<CustomerCutMaterialDetailFormMap> customerCutMaterialDetailFormMaps=customerCutMaterialDetailMapper
					.getCustomerCutMaterialDetailByCustomerCutMaterialId(Integer.parseInt(customerCutMaterialFormMap.getStr("id")));
			
			CustomerCutMaterialDetailFormMap customerCutMaterialDetailFormMap=getFormMap(CustomerCutMaterialDetailFormMap.class);
			for(CustomerCutMaterialDetailFormMap c:customerCutMaterialDetailFormMaps){
				customerCutMaterialDetailFormMap.set("state", 1);
				customerCutMaterialDetailFormMap.put("id", c.getInt("id"));
				customerCutMaterialDetailMapper.editEntity(customerCutMaterialDetailFormMap);
			}
		}
		
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value="customerCutMaterialDetail_deleteEntity")
	@SystemLog(module="客户割料量统计",methods="割料-删除原料子项")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deletecustomerCutMaterialDetailEntity() throws Exception {
		
		String id = getPara("cid");
		System.out.println("你的id"+id);
		CustomerCutMaterialDetailFormMap customerCutMaterialDetailFormMap=getFormMap(CustomerCutMaterialDetailFormMap.class);
		customerCutMaterialDetailFormMap.set("state", 1);
		customerCutMaterialDetailFormMap.put("id", id);
		customerCutMaterialDetailMapper.editEntity(customerCutMaterialDetailFormMap);
		return "success";
	}
	
}
